<template>
  
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="外部商品sku编码" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.organizationProductSku`" :rules="formRules.organizationProductSku" class="mb-0px!">
            <el-input v-model="row.organizationProductSku" placeholder="请输入外部商品sku编码" />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column label="商品" min-width="200">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
            <SpuSelect v-model="row.productId" />
            <!-- <el-input v-model="row.productId" placeholder="请输入商品ID" /> -->
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="商品报价(分)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productPrice`" :rules="formRules.productPrice" class="mb-0px!">
            <el-input v-model="row.productPrice" placeholder="请输入报价，单位：分" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="规格(克)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productWeight`" :rules="formRules.productWeight" class="mb-0px!">
            <el-input v-model="row.productWeight" placeholder="请输入商品规格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="单位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productUnit`" :rules="formRules.productUnit" class="mb-0px!">
            <el-select v-model="row.productUnit" placeholder="请输入商品单位" clearable class="!w-240px">
             <el-option
                   v-for="organization in getIntDictOptions(DICT_TYPE.PRODUCT_PRODUCT_UNIT)"
                   :key="organization.value"
                   :label="organization.label"
                   :value="organization.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="系数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.jinCount`" :rules="formRules.jinCount" class="mb-0px!">
            <el-input v-model="row.jinCount" placeholder="请输入按照斤度换算个数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="每斤原材料价格(分)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.rawMaterialPrice`" :rules="formRules.rawMaterialPrice" class="mb-0px!">
            <el-input v-model="row.rawMaterialPrice" placeholder="请输入每斤原材料价格" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="出成率" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.useRatio`" :rules="formRules.useRatio" class="mb-0px!">
            <el-input v-model="row.useRatio" placeholder="请输入出成率0.7" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="每斤加工费(分)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.processPrice`" :rules="formRules.processPrice" class="mb-0px!">
            <el-input v-model="row.processPrice" placeholder="请输入每斤加工费(分)" />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="运输费用占比" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.transportRatio`" :rules="formRules.transportRatio" class="mb-0px!">
            <el-input v-model="row.transportRatio" placeholder="请输入运输费用占比" />
          </el-form-item>
        </template>
      </el-table-column>
      <!-- <el-table-column label="每斤净菜价格(分)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.jinPrice`" :rules="formRules.jinPrice" class="mb-0px!">
            <el-input v-model="row.jinPrice" placeholder="请输入每斤净菜价格" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加商品报价</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { Api } from '@/api/quotation/quotation'
import SpuSelect from '@/views/mall/product/spu/components/SpuSelect.vue'


const props = defineProps<{
  quotationId: undefined // 报价配置ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  organizationProductSku: [{ required: true, message: '外部商品sku编码不能为空', trigger: 'blur' }],
  quotationId: [{ required: true, message: '报价配置ID不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '商品不能为空', trigger: 'blur' }],
  productWeight: [{ required: true, message: '商品规格不能为空', trigger: 'blur' }],
  productUnit: [{ required: true, message: '商品单位不能为空', trigger: 'blur' }],
  rawMaterialPrice: [{ required: true, message: '每斤原材料价格', trigger: 'blur' }],
  useRatio: [{ required: true, message: '出成率不能为空', trigger: 'blur' }],
  processPrice: [{ required: true, message: '每斤加工费不能为空', trigger: 'blur' }],
  transportRatio: [{ required: true, message: '运输费占比不能为空', trigger: 'blur' }],
  jinCount: [{ required: true, message: '系数不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.quotationId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await Api.getProductListByQuotationId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)


/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    quotationId: undefined,
    productId: undefined,
    productPrice: undefined,
    productWeight: undefined,
    productUnit: undefined,
    jinCount: undefined,
    rawMaterialPrice: undefined,
    processPrice: undefined,
    useRatio: undefined,
    transportRatio: undefined,
    // jinPrice: undefined
  }
  row.quotationId = props.quotationId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

 

defineExpose({ validate, getData })
</script>